package de.hwrberlin.remo.export.textexport;

import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;

import de.hwrberlin.remo.export.xpand.XpandExporter;
import de.hwrberlin.remo.model.KnowledgeBase;

public class SimpleTextTemplateExport extends XpandExporter {

	private static final String BUNDLE_ID      = "de.hwrberlin.remo.export.simpletext";
	private static final String GENERATOR_PATH = "/src/de/hwrberlin/remo/export/textexport/workflow/generator.mwe";

	private static final String TEMPLATE_PATH = 
		"de::hwrberlin::remo::export::textexport::templates::SimpleTextTemplate::ExportExpertSystem";
	
	@Override
	public void exportKnowledgeBase(String modelFilePath, KnowledgeBase base) {
		if(base == null) {
			return;
		}
		
		Bundle thisBundle = Platform.getBundle(BUNDLE_ID);
		URL url = thisBundle.getEntry(GENERATOR_PATH);
		
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		
		FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
		String selectedFilePath = fileDialog.open();
		
		// Has the user select a file and pressed ok?
		if(selectedFilePath != null) {
			String outputFolderPath = getFolderPath(selectedFilePath);
			String fileName = getFileName(selectedFilePath);
			
			// Add to parameter to template path
			String templatePath = TEMPLATE_PATH + "('" + fileName + "')";

			exportModel(url, templatePath, modelFilePath, outputFolderPath);
		}
	}
}
